package com.blockchain.console.service.financial.achieve;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.blockchain.console.service.financial.RechargeQrCodeManager;
import com.blockchain.console.service.financial.entity.RechargeQrCode;
import com.blockchain.console.service.financial.entity.RechargeQrEntity;
import com.blockchain.framework.http.upload.FileStore;
import com.blockchain.framework.http.upload.UploadFile;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.variables.FileType;
import com.blockchain.variables.P2PConst;

public class RechargeQrCodeManagerImp extends AbstractFinancialService implements RechargeQrCodeManager{

	public RechargeQrCodeManagerImp(ServiceResource serviceResource) {
		super(serviceResource);
	}

	public static class RechargeQrCodeManageFactory implements
			ServiceFactory<RechargeQrCodeManager> {

		@Override
		public RechargeQrCodeManager newInstance(ServiceResource serviceResource) {
			return new RechargeQrCodeManagerImp(serviceResource);
		}
	}

	@Override
	public void edit(RechargeQrCode rechargeQrCode) throws Throwable {

		if (rechargeQrCode.isEmpty()) {
			return;
		}
		
		UploadFile firstImage = rechargeQrCode.getFirstImage();

		UploadFile secondImage = rechargeQrCode.getSecondImage();

		UploadFile thirdImage = rechargeQrCode.getThirdImage();

		UploadFile fourthImage = rechargeQrCode.getFourthImage();

		FileStore fileStore = serviceResource.getResource(FileStore.class);
		if (firstImage != null) {
			String imageCode = fileStore.upload(
					FileType.ADVERTISEMENT_IMAGE.ordinal(), firstImage)[0];
			execute(getConnection(P2PConst.DB_USER),"UPDATE T6035 SET F02 = ? WHERE F01=1",imageCode);
		}
		if (secondImage != null) {
			String imageCode = fileStore.upload(
					FileType.ADVERTISEMENT_IMAGE.ordinal(), secondImage)[0];
			execute(getConnection(P2PConst.DB_USER),"UPDATE T6035 SET F03 = ? WHERE F01=1",imageCode);
		}
		if (thirdImage != null) {
			String imageCode = fileStore.upload(
					FileType.ADVERTISEMENT_IMAGE.ordinal(), thirdImage)[0];
			execute(getConnection(P2PConst.DB_USER),"UPDATE T6035 SET F04 = ? WHERE F01=1",imageCode);
		}
		if (fourthImage != null) {
			String imageCode = fileStore.upload(
					FileType.ADVERTISEMENT_IMAGE.ordinal(), fourthImage)[0];
			execute(getConnection(P2PConst.DB_USER),"UPDATE T6035 SET F05 = ? WHERE F01=1",imageCode);
		}
	}

	@Override
	public RechargeQrEntity search() throws Throwable {
		return select(getConnection(P2PConst.DB_USER), new ItemParser<RechargeQrEntity>() {

			@Override
			public RechargeQrEntity parse(ResultSet paramResultSet) throws SQLException {
				RechargeQrEntity entity = new RechargeQrEntity();
				while (paramResultSet.next()) {
					entity.fir = paramResultSet.getString(1);
					entity.sec = paramResultSet.getString(2);
					entity.thir = paramResultSet.getString(3);
					entity.four = paramResultSet.getString(4);
				}
				return entity;
			}
		}, "SELECT F02,F03,F04,F05 FROM T6035 WHERE F01=1");
	}

}
